package cn.hy.today;

// 收集巧克力
class Solution_2735 {

    /**
     * f(i, 0) = nums[i]
     * f(i, k) = min (f(i, k-1) + nums[(i +k) mod n])
     * k (0, n-1),  可以省去k 这一维度
     */
    public long minCost(int[] nums, int x) {
        int n = nums.length;
        int[] f = new int[n];
        System.arraycopy(nums, 0, f, 0, n);
        long ans = getSum(f);
        for (int k = 1; k < n; k++) {
            for (int i = 0; i < n; i++) {
                f[i] = Math.min(f[i], nums[(i + k) % n]);
            }
            ans = Math.min(ans, (long) k * x + getSum(f));
        }
        return ans;
    }

    private long getSum(int[] f) {
        long sum = 0;
        for (int i : f) {
            sum += i;
        }
        return sum;
    }
}